<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('ajax_mod');
    }
    
    public function update_num_online()
    {
        try{          
          $num_online = $this->ajax_mod->getNumOnline();
          $num_users_online = $this->ajax_mod->getNumUsersOnline();
          $num_moderators = $this->ajax_mod->getNumModeratorsOnline();
          $response = array(
                      'ind' => 1,
                      'msg' => "",
                      'num_online' => $num_online,
                      'num_users_online' => $num_users_online,
                      'num_moderators' => $num_moderators
                  );
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'num_online' => 0
            );
        }
        echo json_encode($response);
    }
    
    
    public function update_online_users()
    {
        try{       
          $this->load->model('users_mod');
          $data['online_users'] = $this->users_mod->getOnlineUsers();
          $html = $this->load->view('users/users_list',$data,true);
          $response = array(
                      'ind' => 1,
                      'msg' => "",
                      'html' => $html
                  );
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }
    
    
    
    
    public function change_lang( )
    {      
        try{
          $new_lang = $this->input->post('lang');
          if( $new_lang == 'en' || $new_lang =='de' || $new_lang=='sr' ) {
            $this->session->set_userdata('admin_lang',$new_lang);
            $response = array(
                    'ind' => 1,
                    'msg' => "Language changed"
                );
          } else {
            $this->session->set_userdata('lang','en');
            $response = array(
                    'ind' => 0,
                    'msg' => "Language not changed. Set to English."
                );
          }
          
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        echo json_encode($response);        
    }
       
    
    
    
	/**
     *   Metod za upload slike 
     *    
     */
    public function upload_image( $field_name = 'img', $upload_dir = 'uploads' )            
    {
        
        $this->load->library('file_upload'); // Ukljuci biblioteku
        $ext = array("image/jpeg","image/jpg","image/gif","image/png"); // Ogranici tip datoteka
        
        // Upload path
        $upload_path = "";
        $num_args = func_num_args();
        if( $num_args > 2 ) // Ako ima vise od 2 argumenta onda je definisana duza putanja
          for( $i=1; $i<$num_args; $i++ )
              $upload_path .= func_get_arg($i)."/";
        else
          $upload_path .= $upload_dir;
        
        // Upload slike. Povratna vrednost je array('status'=>0/1, 'msg'=> 'Poruka o gresci', 'file_name'=> '...')
        $response = $this->file_upload->upload_img($this->config->item('base_dir').$upload_path,$field_name,$ext);
              
        
        $response['field_name'] = $field_name; // id hiden polja gde se upisuje naziv datoteke
        if( $response['status'] == 1 ) // Upload uspesan
        {
            $file = $response['file_name']; // naziv datoteke se prosledjuje za akciju brisanja
            $response['html'] = "<div id='div_{$field_name}'><img src='".base_url().$upload_path.'/'.$file."' width='100px' /><strong>". $file . "</strong> - <a href='javascript:void(null)' class='action' onclick='delete_image(\"{$field_name}\",\"{$upload_path}\",\"{$file}\")' > Обриши</a></div>";
        }

        echo json_encode($response);
    }   
    
    public function delete_image( )
    {
        try {            
            $upload_dir = $this->input->post('upload_dir');
            $file = $this->input->post('file');
            if( $file == "" ) {
              throw new Exception("Подаци немогу бити обрисани");
            }
            
            if( true == $this->ajax_mod->deleteImage( $upload_dir, $file ) ) {      
                
                $response = array(
                    'ind' => 1,
                    'msg' => "Слика је успешно обрисана"
                );
            } else {
                $response = array(
                    'ind' => 0,
                    'msg' => "Подаци нису обрисани!"
                );
            }
            
        } catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        echo json_encode($response);
    }
    
    public function update_to_users (){
                
        try{            
          $from = $this->input->post('from');
          $this->load->model('dialogs_mod');
          $to_users = $this->dialogs_mod->getToUsers($from);
          
          $response = array(
                      'ind' => 1,
                      'msg' => "",
                      'to_users' => $to_users
                  );
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'to_users' => ''
            );
        }
        echo json_encode($response);
    }
    public function update_monitor(){
        
        try{       
          $this->load->model('monitor_mod');
          $data['messages'] = $this->monitor_mod->getMessages();
          $html = $this->load->view('monitor/monitor_list',$data,true);
          
          
          $response = array(
                      'ind' => 1,
                      'msg' => "",
                      'html' => $html
                  );
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
        
    }
    
    public function change_active()
    {
        try{       
          
          $id = $this->input->post('id');
          $table = $this->input->post('table');          
          
          switch( $table )
          {
            case 'teams':
              $response = $this->_changeActiveTeams($id);
              break;
            
            default:
              $response = array(
                      'ind' => 0,
                      'msg' => ""
                  );
          }
          
          
        }
        catch (Exception $e) {

            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage(),
                'html' => ''
            );
        }
        echo json_encode($response);
    }
    
    
    private function _changeActiveTeams($id)
    {
        $this->load->model('moderators_mod');
        
        if( true == $this->moderators_mod->changeActiveTeam($id) ) 
        {
          $data['moderator_teams'] = $this->moderators_mod->getTeams();
          $html = $this->load->view('moderators/teams/all_team_list',$data,true);
          $response = array(
                      'ind' => 1,
                      'msg' => lang('team_status_changed'),
                      'html' => $html
                  );
        }
        else
        {
          $response = array(
                      'ind' => 0,
                      'msg' => ""
                  );
        }
        
        return $response;
    }
    
    
    
}

/* End of file ajax.php */
/* Location: ./application/controllers/ajax.php */